package leetcode100;

// TODO 回溯+递归 [经典] 【-】 括号生成
// TODO https://leetcode.cn/problems/generate-parentheses/solution/shou-hua-tu-jie-gua-hao-sheng-cheng-hui-su-suan-fa/

import java.util.ArrayList;
import java.util.List;

public class lc100_0022 {

    List<String> res = new ArrayList<>();

    public List<String> generateParenthesis(int n) {
        dfs(n, n, "", n);
        return res;
    }

    public void dfs(int lRemain, int rRemain, String path, int n) {
        // 递归结束控制器
        if (path.length() == 2*n) {
            res.add(path);
            return;
        }
        // 递归: 有两种选择
        if (lRemain > 0) {
            dfs(lRemain - 1, rRemain, path + "(", n);
        }
        if (lRemain < rRemain) {
            dfs(lRemain, rRemain - 1, path + ")", n);
        }
    }

}